package com.wyd.System.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyd.System.Bean.Students;
import com.wyd.System.Service.StudentsService;
import com.wyd.System.Vo.StudentsVo;
import com.wyd.handler.BusinessException;
import com.wyd.response.Result;
import com.wyd.response.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 学生信息表 前端控制器
 * </p>
 *
 * @author 文永达
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/System/students")
@Api(value = "学生管理")
public class StudentsController {

    @Autowired
    private StudentsService studentsService;

    @GetMapping("/findStudents")
    @ApiOperation(value = "查询所有的学生信息",notes = "")
    public List<Students> findStudents(){
        List<Students> list = studentsService.list();
        return list;
    }

    @GetMapping("/{studentId}")
    @ApiOperation(value = "查询单个学生",notes = "通过ID查询信息")
    public Result getStudentById(@PathVariable("studentId") String studentId){
        List<Students> students =  studentsService.findStudentId(studentId);
        if(students!=null){
            return Result.ok().data("students",students);
        }else {
            throw new BusinessException(ResultCode.STUDENT_NOT_FOUND_EXCEPTION.getCode(),
                    ResultCode.STUDENT_NOT_FOUND_EXCEPTION.getMessage());
        }
    }

//    @GetMapping("/findStudentList")
//    public Result findStudentList(@RequestParam(required = true,defaultValue = "1")Integer current,
//                                  @RequestParam(required = true,defaultValue = "6")Integer size){
//        Page<Students> page = new Page<>(current,size);
//        Page<Students> studentPage = studentsService.page(page);
//        long total = studentPage.getTotal();
//        List<Students> records = studentPage.getRecords();
//        return Result.ok().data("total",total).data("records",records);
//    }

    @PostMapping("/findStudentPage")
    public Result findStudentPage(@RequestParam(required = true,defaultValue = "1")Integer current,
                                  @RequestParam(required = true,defaultValue = "6")Integer size,
                                  @RequestBody StudentsVo studentsVo){
        Page<Students> page = new Page<>(current,size);
        QueryWrapper<Students> wrapper= getWrapper(studentsVo);
        IPage<Students> studentPage = studentsService.findStudentPage(page,wrapper);
        long total = studentPage.getTotal();
        List<Students> records = studentPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }

    private QueryWrapper<Students> getWrapper(StudentsVo studentsVo){
        QueryWrapper<Students> queryWrapper = new QueryWrapper<>();
        if(studentsVo!=null){
            if(!StringUtils.isEmpty(studentsVo.getInstituteName())){
                queryWrapper.eq("institute",studentsVo.getInstituteName());
            }
            if(!StringUtils.isEmpty(studentsVo.getGradeName())){
                queryWrapper.eq("grade",studentsVo.getGradeName());
            }
            if(!StringUtils.isEmpty(studentsVo.getSex())){
                queryWrapper.eq("sex",studentsVo.getSex());
            }
            if(!StringUtils.isEmpty(studentsVo.getSpecialtyName())){
                queryWrapper.eq("specialty",studentsVo.getSpecialtyName());
            }
            if(!StringUtils.isEmpty(studentsVo.getStudentId())){
                queryWrapper.eq("student_id",studentsVo.getStudentId());
            }
            if(!StringUtils.isEmpty(studentsVo.getStudentName())){
                queryWrapper.eq("student_name",studentsVo.getStudentName());
            }
            if(!StringUtils.isEmpty(studentsVo.getStudentClazz())){
                queryWrapper.eq("student_clazz",studentsVo.getStudentClazz());
            }
        }
        return queryWrapper;
    }

    @ApiOperation(value = "添加学生",notes = "添加学生信息")
    @PostMapping("/addStudent")
    public Result addStudent(@RequestBody Students students){
        try{
            studentsService.addStudent(students);
            return Result.ok();
        }catch (Exception e){
            //打印日志
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "删除学生",notes = "删除学生信息，根据学生ID")
    @DeleteMapping("/delete/{id}")
    public Result deleteStudentById(@PathVariable BigDecimal id){
        try {
            studentsService.deleteStudentById(id);
            return Result.ok();
        }catch (Exception e) {
            //打印日志
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "编辑学生",notes = "获取学生的详情，编辑学生信息")
    @GetMapping("/edit/{id}")
    public Result edit(@PathVariable BigDecimal id){
        try {
            studentsService.editStudentById(id);
            return Result.ok();
        }catch (Exception e) {
            //打印日志
            e.printStackTrace();
            return Result.error();
        }
    }
}

